{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b946e454-fe52-4eb1-98eb-2a00a050fe4d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0e9444d9-b8f7-4ff2-8cfc-ed4735b81665",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正常的邮件的文件列表 ['normal-mail1.txt', 'normal-mail2.txt', 'normal-mail3.txt', 'normal-mail4.txt', 'normal-mail5.txt', 'normal-mail6.txt', 'normal-mail7.txt', 'normal-mail8.txt', 'normal-mail9.txt']\n",
      "垃圾的邮件的文件列表 ['spam-mail1.txt', 'spam-mail2.txt', 'spam-mail3.txt', 'spam-mail4.txt', 'spam-mail5.txt', 'spam-mail6.txt', 'spam-mail7.txt', 'spam-mail8.txt', 'spam-mail9.txt']\n",
      "停用词文件内容 ['\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n']\n"
     ]
    }
   ],
   "source": [
    "import os \n",
    "normalFileList=os.listdir(\"/Users/Administrator/Desktop/a/item5-ss-data/normal/\")\n",
    "spamFileList=os.listdir(\"/Users/Administrator/Desktop/a/item5-ss-data/spam/\")\n",
    "print(\"正常的邮件的文件列表\",normalFileList)\n",
    "print(\"垃圾的邮件的文件列表\",spamFileList)\n",
    "stopList=[]\n",
    "for line in open(\"/Users/Administrator/Desktop/a/item5-ss-data/stopwords.txt\",encoding='utf-8'):\n",
    "                 stopList.append(line[len(line)-1])\n",
    "print(\"停用词文件内容\",stopList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f90861dd-5bb5-4e1f-81c1-c5c31e875fb1",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'getWords' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[21], line 5\u001b[0m\n\u001b[0;32m      3\u001b[0m allwords\u001b[38;5;241m=\u001b[39m[]\n\u001b[0;32m      4\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m spamfile \u001b[38;5;129;01min\u001b[39;00m spamFileList:\n\u001b[1;32m----> 5\u001b[0m     words\u001b[38;5;241m=\u001b[39mgetWords(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/Users/Administrator/Desktop/a/item5-ss-data/spam/\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m+\u001b[39mspamfile,stopList)\n\u001b[0;32m      6\u001b[0m     allwords\u001b[38;5;241m.\u001b[39mappend(words)\n\u001b[0;32m      7\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m normalfile \u001b[38;5;129;01min\u001b[39;00m normalFileList:\n",
      "\u001b[1;31mNameError\u001b[0m: name 'getWords' is not defined"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "from itertools import chain\n",
    "allwords=[]\n",
    "for spamfile in spamFileList:\n",
    "    words=getWords(\"/Users/Administrator/Desktop/a/item5-ss-data/spam/\"+spamfile,stopList)\n",
    "    allwords.append(words)\n",
    "for normalfile in normalFileList:\n",
    "    words=getWords(\"/Users/Administrator/Desktop/a/item5-ss-data/normal/\"+spamfile,stopList)\n",
    "    allwords.append(words)\n",
    "print(\"训练集中所有的有效词语列表:\")\n",
    "print(allwords)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "edfae2fe-400e-4bd4-a232-6530229085ab",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'jieba'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[23], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mjieba\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m cut\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mre\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m sub\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mgetWors\u001b[39m(file,stopList):\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'jieba'"
     ]
    }
   ],
   "source": [
    "from jieba import cut\n",
    "from re import sub\n",
    "def getWors(file,stopList):\n",
    "    wordsList=[]\n",
    "    for line in open(file,encoding='utf-8'):\n",
    "        line=line.strip()\n",
    "        line=sub(r'[.【】0-9、--，。！\\~*]','',line)\n",
    "        line=cut(line)\n",
    "        line=filter(lambda word:len(word)>1,line)\n",
    "        wordsList.extend(line)\n",
    "        words=[]\n",
    "        for i in wordList:\n",
    "            if i not in stopList and i .strip()!='' and i!=None:\n",
    "                words.append(i)\n",
    "    return words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "0bd6eb2f-1e19-47ca-916e-4912bc40486a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "vector=[]\n",
    "for words in allwords:\n",
    "    temp=list(map(lambda x:words.count(x),topWords))\n",
    "    vector.append(temp)\n",
    "    vector=np.array(vector)\n",
    "    print(\"10个高频词语在每封邮件中出现的次数:\")\n",
    "    print(vector)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45aee7a8-8f77-4fdb-8ac5-a58b4556ca27",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
